layui.define(["element"], function (exports) {
  var $ = layui.$,
    element = layui.element,
    projectId = layui.data("uuid").key,
    mid = "",
    tabIndex = 0;
  $.ajax({
    url: base_url + "/meeting/ids/" + projectId,
    async: true,
    type: "GET",
    headers: {
      Accept: "application/json; charset=utf-8",
      Authorization: layui.sessionData("userToken").key,
    },
    success: function (data) {
      if (data.code == "200") {
        if (data.data.length > 0) {
          mid = data.data[0].id;
          data.data.forEach((item, index) => {
            var meetingTabItem = ` <div
            class="layui-tab meetingMinutesTable"
            lay-filter="meetingMinutesTable${index}"
          >
            <ul class="layui-tab-title"></ul>
            <div class="layui-tab-content"></div>
          </div>`;
            $(".minutesmeetingTab").html(meetingTabItem);
            element.tabAdd("meetingInfoList", {
              title: item.meetingHeadline,
              id: item.id,
              content: $("#tabPages").html(),
            });
          });
          element.on("tab(meetingInfoList)", function (data) {
            id = $(this).attr("lay-id");
            $(".meetingMinutesTable .layui-tab-title").html("");
            $(".meetingMinutesTable .layui-tab-content").html("");
            initView(id, data.index);
          });
          element.tabChange("meetingInfoList", mid);
        } else {
          $("#meetingMinutesHeader").html("<h3>会议纪要暂无~</h3>");
        }
      } else {
        layer.msg(data.message, { icon: 5 });
      }
    },
  });

  function initView(mid, tabIndex) {
    $.ajax({
      url: base_url + "/meeting/detail/" + mid,
      async: true,
      type: "GET",
      headers: {
        Accept: "application/json; charset=utf-8",
        Authorization: layui.sessionData("userToken").key,
      },
      success: function (data) {
        if (data.code == "200") {
          var meetingDetailsData = data.data.detail;
          $(".meetingHeadline").text(meetingDetailsData.meetingName || "");
          $(".startTime").text(meetingDetailsData.startTime);
          $(".endTime").text(meetingDetailsData.endTime);
          $(".projectName").text(meetingDetailsData.projectName);
          $(".meetingOriginator").text(meetingDetailsData.meetingOriginator);
          $(".meetingOriginatorUnit").text(meetingDetailsData.meetingUnits);
          $(".site").text(meetingDetailsData.roomId);
          $(".remarks").text(meetingDetailsData.remark);
          $(".meetingAttendanceContent").html(meetingDetailsData.ackMsg);
          //会议上传
          $(".downLoadSummary").on("click", function () {
            window.location.href =
              "mfu://roomId=" +
              meetingDetailsData.meetingId +
              "&userNumber=" +
              layui.sessionData("userName").key;
          });
          //会议音频 文字列表
          if (data.data.hasOwnProperty("voices")) {
            var voices = data.data.voices;
            voices.forEach((item, index) => {
              var htmlVido = `<div class="waveform" id="waveform_${tabIndex}_${index}" style="width: 100%; height: 100px"></div>
              <audio class="audio"
              id="audio_${tabIndex}_${index}"
              src="video.mp4"
              controls="" width="100%"
            ><a></a></audio><div class="meetingOpearBtn" data-id="${item.id}">
              <div class="meetingSummaryEdit"><i class="layui-icon layui-icon-edit"></i>保存</div>
            <div class="meetingSummaryDown"><i class="layui-icon layui-icon-download-circle"></i>下载</div>
            </div> <div class="turnText"> <textarea class="turnTextCon turnTextCon${index}">${item.text}</textarea></div>`;
              element.tabAdd("meetingMinutesTable1", {
                title: "音频" + (index + 1), //用于演示
                content: htmlVido,
                id: index,
              });
              element.tabChange("meetingMinutesTable1", 0);
              element.on("tab(meetingMinutesTable1)", function (data) {
                wavesurferFun(data.index, tabIndex);
              });
            });
          }
          if (data.data.hasOwnProperty("files")) {
            var files = data.data.files;
            var str = "";
            files.forEach((item, index) => {
              str += ` <div class="layui-col-md6"><p>${item.fileName}</p><div class="suppleInformationBtn" data-id="${item.id}"><button class="deleteData layui-btn layui-btn-xs technicalReviewBtn layui-btn-danger">删除</button><button class="layui-btn layui-btn-xs technicalReviewBtn technicalRequirement downloadData">下载</button></div></div>`;
              $(".suppleInformation").html(str);
            });
            $(".suppleInformation").on("click", ".downloadData", function () {
              var id = $(this).parent().attr("data-id");
              window.location.href = base_url + "/meeting/download/" + id;
            });
            $(".suppleInformation").on("click", ".deleteData", function () {
              var id = $(this).parent().attr("data-id");
              var removeElement = $(this).parent().parent();
              $.ajax({
                type: "get",
                async: true, //异步执行
                url: base_url + "/meeting/file/delete/" + id,
                headers: {
                  Accept: "application/json; charset=utf-8",
                  Authorization: layui.sessionData("userToken").key,
                },
                contentType: "application/json",
                success: function (data) {
                  if ((data.type = "success")) {
                    layer.confirm("确认删除吗?", function (index) {
                      removeElement.remove();
                      layer.close(index);
                      layer.msg(data.message, { icon: 6 });
                    });
                  } else {
                    layer.msg(data.message, { icon: 5 });
                  }
                },
              });
            });
          }
          $(".meetingSummaryEdit").on("click", function () {
            var id = $(this).parent().attr("data-id");
            $.ajax({
              url: base_url + "/meeting/update/meetingSummary",
              async: true,
              type: "post",
              data: {
                id: id,
                data: document.getElementsByClassName(`turnTextCon${index}`)
                  .innerHTML,
              },
              headers: {
                Accept: "application/json; charset=utf-8",
                Authorization: layui.sessionData("userToken").key,
              },
              success: function (data) {
                if (data.code == "200") {
                  layer.msg(data.message, { icon: 1 });
                }
              },
            });
          });
          // 会议纪要下载
          $(".meetingSummaryDown").on("click", function () {
            var id = $(this).parent().attr("data-id");
            window.location.href = base_url + "/meeting/download/" + id;
          });
        }
      },
    });
  }

  function wavesurferFun(index, tabIndex) {
    console.log(index, tabIndex);
    if (
      document
        .getElementById(`waveform_${tabIndex}_${index}`)
        .getElementsByTagName("wave").length > 0
    ) {
      return;
    }
    var audio = document.getElementById(`audio_${tabIndex}_${index}`);
    var wavesurfer = WaveSurfer.create({
      container: "#waveform_" + tabIndex + "_" + index, //#waveform1  #waveform2 waveform_${tabIndex}_${index}
      hideScrollbar: true, // 横坐标显示
      barWidth: true, // 波形展示为柱状
      height: 100,
      padding: 5,
      backgroundColor: "#EEF3EF",
      waveScale: 0.8,
      mediaType: "audio",
    });
    wavesurfer.load(audio.getAttribute("src"));
    var audios = document.getElementsByTagName("audio");
    // 暂停函数
    function pauseAll() {
      var self = this;
      [].forEach.call(audios, function (i) {
        // 将audios中其他的audio全部暂停
        i !== self && i.pause();
      });
    }
    // 给play事件绑定暂停函数
    [].forEach.call(audios, function (i) {
      i.addEventListener("play", pauseAll.bind(i));
    });

    audio.addEventListener("playing", function () {
      wavesurfer.play();
    });
    audio.addEventListener("pause", function () {
      wavesurfer.pause();
    });
    audio.addEventListener("seeked", function () {});
    audio.addEventListener("muted", function () {});
  }

  exports("meetingList", {});
});
